package com.wdb007.venus.web.api.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wdb.exchange.constant.EventEnum;
import com.wdb.exchange.model.Event;
import com.wdb007.venus.svc.facade.CouponService;
import com.wdb007.venus.svc.facade.OrderService;
import com.wdb007.venus.svc.facade.exception.BizException;

@Component
public class ApiListenHandler {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private CouponService couponService;
	
	public void process(Event event) {
		EventEnum eventEnum = event.event;

		switch (eventEnum) {
			case DISCARD:
				Long orderTransId = Long.valueOf(event.extra);
				try {
					orderService.discardOrderTrans(orderTransId);
				} catch (BizException e) {
					logger.error("报废图书异常,流水号:[" + orderTransId + "]", e);
				}
				break;
			case AWARD_PRESENT:
				String couponRuleId = event.extra;
				Long userId = event.userId;
				try {
					couponService.draw(userId, couponRuleId);
				} catch (BizException e) {
					logger.error("下发优惠券异常,userId:" + userId + ", couponRuleId: " + couponRuleId, e);
				}
				break;
			default:
				break;
		}
	}
}
